import request from '@/utils/request'

interface ApiResponse<T = any> {
  code: number;
  msg: string;
  data: T;
}

// Assuming UserInfo structure from backend
interface UserInfo {
  id: number;
  username: string;
  realName: string;
  phone: string;
  email: string;
  avatar: string;
  // Add other fields as necessary
}

export function getInfo(): Promise<ApiResponse<UserInfo>> {
  return request({
    url: '/api/user/info',
    method: 'get'
  })
}

export function getUserList(params?: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user/list',
    method: 'get',
    params
  })
}

export function getUserPage(params?: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user/page',
    method: 'get',
    params
  })
}

// 根据部门ID获取用户列表（分页）
export function getUserPageByDept(params: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user/pageByDept',
    method: 'get',
    params
  })
}

// 新增用户
export function addUser(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user',
    method: 'post',
    data
  })
}

// 修改用户
export function updateUser(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user',
    method: 'put',
    data
  })
}

// 删除用户
export function deleteUser(id: number): Promise<ApiResponse<any>> {
  return request({
    url: `/api/user/${id}`,
    method: 'delete'
  })
}

// 更新个人信息
export function updateProfile(data: { realName: string; phone: string; email: string }): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user/profile',
    method: 'put',
    data
  })
}

// 修改密码
export function updatePassword(oldPassword: string, newPassword: string): Promise<ApiResponse<any>> {
  return request({
    url: '/api/user/password',
    method: 'put',
    params: { oldPassword, newPassword } // Use params for query parameters
  })
}

// 上传头像
export function uploadAvatar(file: File): Promise<ApiResponse<{ avatarUrl: string }>> {
  const formData = new FormData();
  formData.append('file', file);
  return request({
    url: '/api/user/avatar',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}